package com.example.administrator.myproject.Model;

import android.icu.lang.UScript;
import android.util.Log;

import com.example.administrator.myproject.GlobalConfig;
import com.example.administrator.myproject.IMethod.GitHubNetworkInterface;
import com.example.administrator.myproject.beans.Result;
import com.example.administrator.myproject.beans.UserInfo;

import java.io.IOException;
import java.util.List;

import retrofit2.Call;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.http.Field;
import rx.Observable;
import rx.Subscriber;

/**
 * Created by Administrator on 2016-11-30.
 */
public class UserModel {
    private int mCount;

    private boolean mBoolean = true;

    public UserModel() {

    }
    public Observable<List<UserInfo>> getDataFromGithub(String name, String psw) {
        Observable mOservable = Observable.create(new Observable.OnSubscribe<List<UserInfo>>() {
            @Override
            public void call(Subscriber<? super List<UserInfo>> subscriber) {
                Retrofit retrofit = new Retrofit.Builder()
                        .baseUrl(GlobalConfig.baseUrl)
                        .addConverterFactory(GsonConverterFactory.create())//添加 json 转换器
                        .addCallAdapterFactory(RxJavaCallAdapterFactory.create())//添加 RxJava 适配器
                        .build();
                //这里用到一个创建一个接口，来调用方法,请求网络
                //Create an implementation of the API endpoints defined by the {@code service} interface.
                GitHubNetworkInterface gitHub = retrofit.create(GitHubNetworkInterface.class);
                Call<Result> call = gitHub.getUserInfo("S04-0011s", "123");
                Result  list = null;
                try {
                    //execute() - 同步操作
                    //enqueue(Callback<T> callback) - 异步操作－用不到了，因为在观察者模式里面已经设置了在非ui线程中执行操作
                    //最后返回一个反序列化的结果
                    list = call.execute().body();
                    Log.d("蘇增瑞", "call: "+list.getStatus());
                } catch (IOException e) {
                    e.printStackTrace();
                }
              //  subscriber.onNext(Result);
                subscriber.onCompleted();
            }
        });
        return mOservable;
    }
}
